package decisionLines.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import decisionLines.model.DecisionLinesEvent;
import decisionLines.view.Application;

/**
 * ChoiceController Class
 */
public class ChoiceController implements ActionListener {
	int position;
	Application view;
	DecisionLinesEvent event;
	
	/**
	 * Constructor: Create the controller object
	 * @param event - DecisionLinesEvent, the DecisionLinesEvent class to be used in controlling objects
	 * @param position - Int, the position of the object to be controlled
	 * @param view - Application, the physical object being affected
	 */
	public ChoiceController (DecisionLinesEvent event, int position, Application view){
		this.position = position;
		this.event = event;
		this.view = view;
	}
	
	/**
	 * Method: KeyPressed, event to monitor key presses
	 * @param ae - ActionEvent, capture enter key being pressed
	 */
	@Override
	public void actionPerformed(ActionEvent ae) {
		
		// If the key being pressed is the enter key then set the choice for the line
		String value = view.getTextFields()[position].getText();
		this.act(value);
	}
	
	/**
	 * Method: Action
	 * @param c - String of the action
	 */
	void act(String c){
		if(!event.isReady() && !event.setChoice(position, c)){
			System.out.println("Choice must contain at least one character. No choices allowed after event is ready.");
		} else if (!event.isReady() && event.setChoice(position, c)) {
			System.out.println("Choice added to model [" + position + "]: " + event.getLines().get(position).getChoice());
		}
		if(event.isReady()){
			System.out.println("Event is ready!");
			
			//For each choice disable editability for each text field
			for(int i=0; i<event.getNumChoices();i++){
				view.getTextFields()[i].setEditable(false);
			}
		}
	}
}